using UnityEngine;

public class Enemy : MonoBehaviour
{
    public float speed = 3.0f;
    public int hp = 1;

    // 控制敌人受伤动画的变量
    // 帧率（每秒播放的帧数，如 10 表示每秒切换 10 张图片）
    // 受伤瞬间的动画状态
    private bool _isPlayDamageAnimation = false;
    public int frameRate = 2;
    public Sprite[] damageSprites;
    public SpriteRenderer spriteRenderer;
    private float _timer = 0;
    private int _currentFrame = 0;

    private Sprite _idleSprite;

    // 控制敌人死亡动画
    private bool _isPlayDeadAnimation = false;
    public Sprite[] deadSprites;

    // 敌人分数奖励
    public int score = 10;

    // 爆炸声音播放器
    public AudioSource boomAudio;


    void Start()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        _idleSprite = spriteRenderer.sprite;
        // 内部可以拖动直接赋值，类似整个资源中选择预制体赋值（Spawner引用子弹） 或 场景中必然存在的对象赋值（GameManager引用UI）
        // boomAudio = GetComponents<AudioSource>()[0];
    }

    void Update()
    {
        UpdateMove();
        PlayDamageAnimationUpdate();
        PlayDeadAnimationUpdate();
    }

    void PlayDeadAnimationUpdate()
    {
        if (_isPlayDeadAnimation)
        {
            _timer = _timer + Time.deltaTime;
            if (_timer > 1f / frameRate)
            {
                _timer = _timer - Time.deltaTime;
                _currentFrame++;
                if (_currentFrame > deadSprites.Length)
                {
                    //结束播放死亡动画
                    spriteRenderer.enabled = false;
                    Destroy(this.gameObject, 2f);
                }
                else
                {
                    spriteRenderer.sprite = deadSprites[_currentFrame - 1];
                }
            }
        }
    }

    void PlayDamageAnimationUpdate()
    {
        if (_isPlayDamageAnimation)
        {
            _timer = _timer + Time.deltaTime;
            if (_timer > 1f / frameRate)
            {
                _timer = _timer - Time.deltaTime;
                _currentFrame++;
                if (_currentFrame > damageSprites.Length)
                {
                    //结束播放受伤动画
                    ResetIdleSprite();
                }
                else
                {
                    spriteRenderer.sprite = damageSprites[_currentFrame - 1];
                }
            }
        }
    }

    void ResetIdleSprite()
    {
        _currentFrame = 0;
        _timer = 0;
        _isPlayDamageAnimation = false;
        spriteRenderer.sprite = _idleSprite;
    }

    void UpdateMove()
    {
        if (hp > 0)
        {
            transform.Translate(Vector3.down * speed * Time.deltaTime);
        }

        // check and destroy enemy
        if (transform.position.y < -5.5f)
        {
            Destroy(this.gameObject);
        }
    }

    public void TakeDamage()
    {
        TakeDamage(1);
    }

    public void TakeDamage(int damage)
    {
        // 防止死亡动画播放期间继续执行碰撞逻辑
        if (hp <= 0)
        {
            return;
        }

        hp = hp - damage;
        if (hp <= 0)
        {
            //死亡
            Death();
        }
        else
        {
            //持续受伤
            ResetIdleSprite();
            _isPlayDamageAnimation = true;
        }
    }

    void Death()
    {
        ResetIdleSprite();
        _isPlayDeadAnimation = true;
        GetComponent<Collider2D>().enabled = false;
        //加分
        GameManager.Instance.AddScore(score);
        // 播放死亡音效
        boomAudio.Play();
    }
}